package com.zj.common.xml;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.zj.common.TiripPackage;

public class XmlUtil {
	private static String PREFIX_CDATA = "<![CDATA[";
	private static String SUFFIX_CDATA = "]]>";

	public static String toXml(Object obj, String encoding) {
		XStream xs = null;
		xs = new XStream(new XppDriver() {
			@Override
			public HierarchicalStreamWriter createWriter(Writer out) {
				return new PrettyPrintWriter(out) {
					protected void writeText(QuickWriter writer, String text) {
						if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
							writer.write(text);
						} else {
							super.writeText(writer, text);
						}
					}
				};
			}
		});
		xs.autodetectAnnotations(true);
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		Writer writer;
		try {
			writer = new OutputStreamWriter(outputStream, encoding);
			writer.write("<?xml version=\"1.0\" encoding=" + "\"" + encoding + "\"?>");
			xs.toXML(obj, writer);
			String xml = outputStream.toString(encoding);
			return xml;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	public static String toXmlNoHead(Object obj) {
		XStream xs = new XStream();
		xs.autodetectAnnotations(true);
		xs.aliasSystemAttribute(null, "class"); // 去掉 class 属性  
		return xs.toXML(obj);
	}
	
	public static <T> T  toBean(String xmlStr,Class<T> cls,Class head,Class body){
		String str = xmlStr.replace("<head>","<head class='"+head.getName()+"'>").
				replace("<body>","<body class='"+body.getName()+"'>");		
		//注意：不是new Xstream(); 否则报错：java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
		XStream xstream=new XStream(new DomDriver());
		xstream.processAnnotations(cls);
		@SuppressWarnings("unchecked")
		T obj=(T)xstream.fromXML(str);
		return obj;			
	} 
	
	public static <T> T  toBean(String xmlStr,Class<T> cls){
		//注意：不是new Xstream(); 否则报错：java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
		XStream xstream=new XStream(new DomDriver());
		xstream.processAnnotations(cls);
		@SuppressWarnings("unchecked")
		T obj=(T)xstream.fromXML(xmlStr);
		return obj;			
	} 



	public static String addCdata(String str) {
		return PREFIX_CDATA + str + SUFFIX_CDATA;
	}

	public static String subCdata(String str) {

		return str.replace(PREFIX_CDATA, "").replace(SUFFIX_CDATA, "");
	}
	
	public static String subCdata(TiripPackage tiripPackage) {
		String str = tiripPackage.getBusinessContent().getSubPackage().getContent();
		return str.replace(PREFIX_CDATA, "").replace(SUFFIX_CDATA, "");
	}

}
